package vip.mengqin.diandezhun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;

import java.util.List;

import vip.mengqin.diandezhun.R;
import vip.mengqin.diandezhun.responce.RechargeData;

/**
 * Created by Administrator on 2018/8/22.
 */

public class RechargeCenter_ValueListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    List<RechargeData> list;
    CommCallBack callBack;

    int select = -1 ;

    public RechargeCenter_ValueListAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setData(List<RechargeData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_recharge_center_value, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final RechargeData info = list.get(position);

        if(select==-1 && position==0)
        {
            select = position;
            if (callBack != null) {
                callBack.onResult("change");
            }
        }

        //数据绑定逻辑
        viewHolder.tv_name.setText(info.title);
        viewHolder.tv_price.setText("￥" + info.price);
        viewHolder.tv_remark.setText(info.description);

        if (position == select) {
            viewHolder.rl_content.setBackgroundResource(R.drawable.shape_recharge_valueitem_bg_select);
        } else {
            viewHolder.rl_content.setBackgroundResource(R.drawable.shape_recharge_valueitem_bg_unselect);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select = position;
                notifyDataSetChanged();
                if (callBack != null) {
                    callBack.onResult("change");
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_price, tv_remark;
        RelativeLayout rl_content;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_remark = itemView.findViewById(R.id.tv_remark);
            rl_content = itemView.findViewById(R.id.rl_content);
        }
    }
}
